namespace DotNetNuke.Security.Permissions.Controls
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Security.Permissions;
    using DotNetNuke.Security.Roles;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Web.UI.WebControls;

    public class FolderPermissionsGrid : PermissionsGrid
    {
        private string _FolderPath = "";
        private ArrayList _FolderPermissions;
        private ArrayList _Permissions;
        private ArrayList _roles;

        private FolderPermissionInfo FolderHasPermission(int PermissionID, ArrayList arrFolderPermissions, string RoleName)
        {
            int refIntHelperL0 = arrFolderPermissions.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                FolderPermissionInfo objFolderPermission = (FolderPermissionInfo) arrFolderPermissions[i];
                if ((objFolderPermission.RoleName == RoleName) & (PermissionID == objFolderPermission.PermissionID))
                {
                    return objFolderPermission;
                }
            }
            return null;
        }

        public override void GenerateDataGrid()
        {
            this.Columns.Clear();
            if (this.FolderPath == "")
            {
                this.FolderPath = this.ViewState["FolderPath"].ToString();
            }
            this.SetPermissions();
            BoundColumn textCol = new BoundColumn();
            textCol.HeaderText = "";
            textCol.DataField = "Roles";
            this.Columns.Add(textCol);
            int refIntHelperL0 = this._Permissions.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                PermissionInfo objPermission = (PermissionInfo) this._Permissions[i];
                CheckBoxColumn checkCol = new CheckBoxColumn();
                string locName = DotNetNuke.Services.Localization.Localization.GetString(objPermission.PermissionName + ".Permission", "~/App_GlobalResources/GlobalResources.resx");
                checkCol.HeaderText = Interaction.IIf(locName != "", locName, objPermission.PermissionName).ToString();
                checkCol.DataField = objPermission.PermissionName;
                this.Columns.Add(checkCol);
            }
            this.DataSource = this.GetPermissionsDataTable();
            this.DataBind();
            this.DynamicColumnAdded = true;
        }

        private DataTable GetPermissionsDataTable()
        {
            int i;
            DataTable dt = new DataTable();
            try
            {
                DataColumn col = new DataColumn("Roles");
                dt.Columns.Add(col);
                int refIntHelperL0 = this._Permissions.Count - 1;
                i = 0;
                while (i <= refIntHelperL0)
                {
                    PermissionInfo objPerm = (PermissionInfo) this._Permissions[i];
                    col = new DataColumn(objPerm.PermissionName);
                    dt.Columns.Add(col);
                    i++;
                }
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                string s = ex.Message;
                
            }
            DataRow row = dt.NewRow();
            int refIntHelperL1 = this._roles.Count - 1;
            for (i = 0; i <= refIntHelperL1; i++)
            {
                RoleInfo role = (RoleInfo) this._roles[i];
                row = dt.NewRow();
                row[0] = DotNetNuke.Services.Localization.Localization.LocalizeRole(role.RoleName);
                int refIntHelperL2 = this._Permissions.Count - 1;
                for (int j = 0; j <= refIntHelperL2; j++)
                {
                    string key;
                    PermissionInfo objPerm = (PermissionInfo) this._Permissions[j];
                    string CheckBoxEnabled = "Y";
                    FolderPermissionInfo objFolderPermission = this.FolderHasPermission(objPerm.PermissionID, this._FolderPermissions, role.RoleName);
                    if (objFolderPermission != null)
                    {
                        if (role.RoleID == PortalController.GetCurrentPortalSettings().AdministratorRoleId)
                        {
                            CheckBoxEnabled = "N";
                        }
                        key = "|" + Convert.ToString(objPerm.PermissionID) + "|" + Convert.ToString(objFolderPermission.FolderPermissionID) + "|" + role.RoleName + "|" + role.RoleID.ToString() + "|" + CheckBoxEnabled;
                        if (objFolderPermission.AllowAccess | (CheckBoxEnabled == "N"))
                        {
                            row[j + 1] = "True" + key;
                        }
                        else
                        {
                            row[j + 1] = "False" + key;
                        }
                    }
                    else
                    {
                        if (role.RoleID == PortalController.GetCurrentPortalSettings().AdministratorRoleId)
                        {
                            CheckBoxEnabled = "N";
                        }
                        key = "|" + Convert.ToString(objPerm.PermissionID) + "||" + role.RoleName + "|" + role.RoleID.ToString() + "|" + CheckBoxEnabled;
                        row[j + 1] = "False" + key;
                    }
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

        private void SetPermissions()
        {
            int intPortalID;
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            this._roles = new RoleController().GetPortalRoles(_portalSettings.PortalId);
            RoleInfo r = new RoleInfo();
            r.RoleID = int.Parse("-3");
            r.RoleName = "Unauthenticated Users";
            this._roles.Add(r);
            r = new RoleInfo();
            r.RoleID = int.Parse("-1");
            r.RoleName = "All Users";
            this._roles.Add(r);
            this._roles.Reverse();
            this._roles.Sort(new RoleComparer());
            FolderPermissionController objFolderPermissionController = new FolderPermissionController();
            if (_portalSettings.ActiveTab.ParentId == _portalSettings.SuperTabId)
            {
                intPortalID = Null.NullInteger;
            }
            else
            {
                intPortalID = _portalSettings.PortalId;
            }
            this._FolderPermissions = objFolderPermissionController.GetFolderPermissionsByFolder(intPortalID, this.FolderPath);
            this._Permissions = new PermissionController().GetPermissionsByFolder(intPortalID, this.FolderPath);
        }

        public string FolderPath
        {
            get
            {
                if (this._FolderPath != "")
                {
                    return this._FolderPath;
                }
                return this.ViewState["FolderPath"].ToString();
            }
            set
            {
                this._FolderPath = value;
                this.ViewState["FolderPath"] = value;
            }
        }

        public FolderPermissionCollection Permissions
        {
            get
            {
                IEnumerator refObjectHelperL0=null;
                PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                FolderPermissionCollection arrFolderPermissions = new FolderPermissionCollection();
                RoleController objRoleController = new RoleController();
                try
                {
                    refObjectHelperL0 = this.Items.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        DataGridItem dgi = (DataGridItem) refObjectHelperL0.Current;
                        int refIntHelperL0 = dgi.Cells.Count - 1;
                        for (int i = 1; i <= refIntHelperL0; i++)
                        {
                            if (dgi.Cells[i].Controls.Count > 0)
                            {
                                FolderPermissionInfo objFolderPermission = new FolderPermissionInfo();
                                CheckBox cb = (CheckBox) dgi.Cells[i].Controls[0];
                                if (cb.Checked)
                                {
                                    RoleInfo objRoleInfo;
                                    string[] Settings = Strings.Split(cb.ID, "|", -1, CompareMethod.Binary);
                                    objFolderPermission.PermissionID = Convert.ToInt32(Settings[1]);
                                    string refStringHelperL0 = Settings[3];
                                    if (refStringHelperL0 == "All Users")
                                    {
                                        objRoleInfo = new RoleInfo();
                                        objRoleInfo.RoleID = Convert.ToInt32("-1");
                                        objRoleInfo.RoleName = "All Users";
                                        objRoleInfo.PortalID = _portalSettings.PortalId;
                                    }
                                    else if (refStringHelperL0 == "Unauthenticated Users")
                                    {
                                        objRoleInfo = new RoleInfo();
                                        objRoleInfo.RoleID = Convert.ToInt32("-3");
                                        objRoleInfo.RoleName = "Unauthenticated Users";
                                        objRoleInfo.PortalID = _portalSettings.PortalId;
                                    }
                                    else
                                    {
                                        objRoleInfo = objRoleController.GetRoleByName(_portalSettings.PortalId, Settings[3]);
                                    }
                                    objFolderPermission.RoleID = objRoleInfo.RoleID;
                                    objFolderPermission.AllowAccess = true;
                                    if (Settings[2] == "")
                                    {
                                        objFolderPermission.FolderPermissionID = -1;
                                    }
                                    else
                                    {
                                        objFolderPermission.FolderPermissionID = Convert.ToInt32(Settings[2]);
                                    }
                                    objFolderPermission.FolderPath = this.FolderPath;
                                    arrFolderPermissions.Add(objFolderPermission);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                return arrFolderPermissions;
            }
        }
    }
}

